package com.atguigu.gmall.activity.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.activity.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author WYL
 * @Date 2021/7/15 14:21
 * @Description: 商品秒杀service实现类
 * @Version 1.0.1
 */
@Service
public class SeckillGoodsServiceImpl
        extends ServiceImpl<SeckillGoodsMapper,SeckillGoods>
        implements SeckillGoodsService  {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询当前时间段所有商品列表
     *
     * @param time 当前开始时间段
     * @return List<SeckillGoods>
     */
    @Override
    public List<SeckillGoods> findAll(String time) {
 //       List<SeckillGoods> seckillGoodsList = new ArrayList<>();
        //从redis获取当前时间段所有商品列表
        String s = stringRedisTemplate.boundHashOps(RedisConst.SECKILL_GOODS + time).values().toString();
//        for (Object value : values) {
//            String stringSeckillGoods = value.toString();
//            SeckillGoods seckillGoods = JSONObject.parseObject(stringSeckillGoods, SeckillGoods.class);
//            seckillGoodsList.add(seckillGoods);
//        }
        //转换数据
        List<SeckillGoods> seckillGoodsList = JSON.parseArray(s, SeckillGoods.class);
        return seckillGoodsList;
    }

    /**
     * 查询商品详情
     *
     * @param time    当前开始时间段
     * @param GoodsId 秒杀商品Id
     * @return SeckillGoods
     */
    @Override
    public SeckillGoods getSeckillGoodsById(String time, String GoodsId) {
        SeckillGoods SeckillGoods = (SeckillGoods)stringRedisTemplate.
                boundHashOps(RedisConst.SECKILL_GOODS + time).get(GoodsId);
        return SeckillGoods;
    }
}
